package 牛客;

import java.util.*;

/*括号匹配*/
public class bracketsMatching {
    /**
     *
     * @param s string字符串
     * @return bool布尔型
     */
    public static boolean IsValidExp (String s) {
        Stack<Character> stack = new Stack<>();
        for(int i=0;i<s.length();i++){
            char temp = s.charAt(i);
            if (stack.empty()){
                stack.push(temp);
                continue;
            }
            char peak = stack.peek();
            if(peak == '('){
                if(temp == ')'){
                    stack.pop();
                    continue;
                }
                if(temp == ']' || temp == '}')
                    return false;
            }else if(peak == '{'){
                if(temp == '}'){
                    stack.pop();
                    continue;
                }
                if(temp == ']' || temp == ')')
                    return false;
            }else if(peak == '['){
                if(temp == ']'){
                    stack.pop();
                    continue;
                }
                if(temp == ')' || temp == '}')
                    return false;
            }
            stack.push(temp);
        }
        return stack.empty();
    }

    public static void main(String[] args) {
        System.out.println(IsValidExp("[][][]{"));
    }
}